<?php
/**
 * @file $HeadURL: productcategoryproperty $
 * @author $Author: LDD
 * @date $Date: 2014-12-22
 * @brief 
 */
namespace haibao\cms\business;

use haibao\cms\Config;

use \haibao\cms\data\ProductCategoryProperty as PropertyData;
use \haibao\cms\model\filter\ProductCategoryProperty as CategoryPropertyFilter;
use \haibao\cms\business\ProductCategoryDefine as CategoryBusiness;
use \haibao\frame\data\query\Condition ;

class ProductCategoryProperty extends \haibao\cms\business\Base{
	public  static $dataType = array(
			\haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_INPUT_TEXT =>'输入框',
			\haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_SELECT =>'下拉框',
			\haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_TEXTAREA =>'文本框',
			\haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_COIN =>'货币'
	);
	/**
	 * @param string $propertyName
	 * @param int $categoryId
	 * @param int $propertyId
	 */
	public static function hasSamePropertyName($propertyName,$categoryId,$propertyId = null){
		if(!$propertyName){
			throw new BusinessException('属性名称不存在',Base::DATA_EMPTY_NAME);
		}
		if(!$categoryId){
			throw new BusinessException('分类id不存在',Base::DATA_EMPTY_ID);
		}
		$propertyFilter = new CategoryPropertyFilter();
		if($propertyId){
			$propertyFilter->where(CategoryPropertyFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $propertyId);
		}
		$propertyFilter->where(CategoryPropertyFilter::CONDITION_FIELD_NAME_CATEGORY_ID, Condition::CONDITION_EQUAL, $categoryId);
		$propertyFilter->where(CategoryPropertyFilter::CONDITION_FIELD_NAME_PROPERTY_NAME, Condition::CONDITION_EQUAL, $propertyName);
		$propertyFilter->where(CategoryPropertyFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\ProductCategoryProperty::STATUS_ENABLE);
		$data = new PropertyData();
		if($data->getAll($propertyFilter)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 添加产品属性
	 * @param (array)\haibao\cms\model\data\ProductCategoryProperty $categoryPropertyModelArr
	 */
	public static function addCategotyProperty($categoryPropertyModelArr){
		self::checkOperation(self::FUNC_CODE_PRODUCT_PROPERTY_ADD);
		if($categoryPropertyModelArr){
			foreach($categoryPropertyModelArr as $key=>$categoryPropertyModel){
				if(!$categoryPropertyModel && !($categoryPropertyModel instanceof \haibao\cms\model\data\ProductCategoryProperty)){
					throw new BusinessException('产品属性不存在',Base::DATA_EMPTY);
				}
				if(!$categoryPropertyModel->PropertyName){
					throw new BusinessException('属性名称不能为空',Base::DATA_EMPTY_NAME);
				}
				if(!$categoryPropertyModel->CategoryId){
					throw new BusinessException('分类不能为空',Base::DATA_EMPTY_ID);
				}
				if(self::hasSamePropertyName($categoryPropertyModel->PropertyName, $categoryPropertyModel->CategoryId)){
					throw  new BusinessException('此分类下属性名已存在',Base::DATA_ERROR_NAME_EXIST);
				}
				if(!$categoryPropertyModel->DataType){
					throw  new BusinessException('属性类型不能为空',Base::DATA_EMPTY);
				}
				switch($categoryPropertyModel->DataType){
					case \haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_INPUT_TEXT:
					case \haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_TEXTAREA:
						$categoryPropertyModel->DefaultValue = $categoryPropertyModel->DataValue;
						break;
					case \haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_SELECT:
						$categoryPropertyModel->SelectDataValue = $categoryPropertyModel->DataValue;
						break;
					case \haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_COIN:
						$categoryPropertyModel->SelectDataValue = json_encode(array_values(\haibao\cms\Config::$monthProperty));
						break;
				}
				
				$categoryPropertyModel->CreateBy = self::getCurrentUser()->Id;
				$categoryPropertyModel->CreateTime = new \DateTime();
				$categoryPropertyModel->Status = \haibao\cms\model\data\ProductCategoryDefine::STATUS_ENABLE;
				if(!$key){
					$orderNo = self::getCurrentMaxOrder($categoryPropertyModel->CategoryId);
				}else{
					$orderNo++;
				}
				$categoryPropertyModel->OrderNo = $orderNo;
			}
			$data = new PropertyData();
			$data->addAll($categoryPropertyModelArr);
		}
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_PROPERTY_ADD, $categoryPropertyModel->toArray());
	}
	
	/**
	 * 保存属性
	 */
	public static function saveCategoryProperty($categoryPropertyModel){
		self::checkOperation(self::FUNC_CODE_PRODUCT_PROPERTY_SAVE);
		
		if(!$categoryPropertyModel && !($categoryPropertyModel instanceof \haibao\cms\model\data\ProductCategoryProperty)){
			throw new BusinessException('产品属性不存在',Base::DATA_EMPTY);
		}
		if(!$categoryPropertyModel->Id){
			throw new BusinessException('属性id不能为空',Base::DATA_EMPTY_ID);
		}
		if(!$categoryPropertyModel->PropertyName){
			throw new BusinessException('属性名称不能为空',Base::DATA_EMPTY_NAME);
		}
		if(self::hasSamePropertyName($categoryPropertyModel->PropertyName, $categoryPropertyModel->CategoryId,$categoryPropertyModel->Id)){
			throw  new BusinessException('此分类下属性名已存在');
		}
		if(!$categoryPropertyModel->DataType){
			throw  new BusinessException('属性类型不能为空');
		}
		switch($categoryPropertyModel->DataType){
			case \haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_INPUT_TEXT:
			case \haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_TEXTAREA:
				$categoryPropertyModel->DefaultValue = $categoryPropertyModel->DataValue;
				break;
			case \haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_SELECT:
				$categoryPropertyModel->SelectDataValue = $categoryPropertyModel->DataValue;
				break;
			case \haibao\cms\model\data\ProductCategoryProperty::DATA_TYPE_COIN:
				$categoryPropertyModel->SelectDataValue = json_encode(array_values(\haibao\cms\Config::$monthProperty));
				break;
		}
		
		//self::setCategoryProperty($categoryPropertyModel,false);
		$data = new PropertyData();
		$data->save($categoryPropertyModel);
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_PROPERTY_SAVE, $categoryPropertyModel->toArray());
	}
	/**
	 * 根据产品属性id查找
	 */
	public static function getPropertyById($propertyId){
		if(!$propertyId){
			throw new BusinessException('属性id不能为空',Base::DATA_EMPTY_ID);
		}
		$data = new PropertyData();
		return $data->getPropertyById($propertyId);
	}
	
	/**
	 * 获取当前分类下最大的排序
	 */
    public static function getCurrentMaxOrder($categoryId){
    	$data = new PropertyData();
    	$max = $data->getMaxOrder($categoryId);
    	return !$max ? \haibao\cms\model\data\ProductCategoryProperty::DEFAULT_START_ORDERNO :$max + 1;
    }
    
    /**
     * 设置子分类的属性
     */
    public static function setCategoryProperty($categoryPropertyModel,$isAdd = true){
    	if($categoryPropertyModel->SnycSub == \haibao\cms\model\data\ProductCategoryProperty::IS_SYNC_SUB){
    		$childCategoryIds = CategoryBusiness::getAllChildrenCategory($categoryPropertyModel->CategoryId);
    		$subCategoryProperty = array();
    		if($childCategoryIds){
    			foreach($childCategoryIds as $key=>$childCategory){
    				if(!self::hasSamePropertyName($categoryPropertyModel->PropertyName,$childCategory['Id'])){
    					$propertyModel = clone $categoryPropertyModel;
    					if(!$isAdd){
    						$categoryPropertyModel->CreateBy = self::getCurrentUser()->Id;
    						$categoryPropertyModel->CreateTime = new \DateTime();
    						$categoryPropertyModel->Status = \haibao\cms\model\data\ProductCategoryDefine::STATUS_ENABLE;
    					}
    					$propertyModel->CategoryId = $childCategory['Id'];
    					$propertyModel->OrderNo = self::getCurrentMaxOrder($childCategory['Id']);
    					array_push($subCategoryProperty, $propertyModel);
    				}
    			}
    		}
    		$categoryPropertyModel->SubCategoryProperty = $subCategoryProperty ;
    		return $categoryPropertyModel;
    	}
    }
    public static function getPropertyByFilter($filter){
    	$data = new PropertyData();
    	return $data->getAll($filter);
    }
    
/**
	 * 删除属性
	 */
	public static function deletePropertyById($propertyId){
		self::checkOperation(self::FUNC_CODE_PRODUCT_PROPERTY_DELETE);
	
		if(!$propertyId){
			throw new BusinessException('属性不能为空',self::DATA_EMPTY_ID);
		}
		$data = new PropertyData();
	
		$arrSet = array();
		$arrSet['Status']  = \haibao\cms\model\data\ProductCategoryProperty::STATUS_DISABLE;
		$data->deleteProperty($propertyId,$arrSet);
	
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_PROPERTY_DELETE, array('Id' =>$propertyId));
	} 
}